package com.example.demo.zaqizabatest.多线程相关;

import lombok.SneakyThrows;

/**
 * @ClassName ABC
 * @Author lys4134
 * @Date 2021/09/01 17:41:14
 * @Description ABC
 * @Version 1.0
 **/
@SuppressWarnings("AlibabaAvoidManuallyCreateThread")
public class ABC1{
    private static volatile int i = 0;
    private static Object object = new Object();
    public static void main(String[] args) {

        new Thread(ABC1::printA).start();
        new Thread(ABC1::printB).start();
        new Thread(ABC1::printC).start();
    }


    @SneakyThrows
    private static void printA()  {
        while (true){
            synchronized (object){
                if (i%3 == 0){
                    System.out.print("A");
                    i++;
                    object.notifyAll();
                } else {
                    object.wait();
                }
            }
        }
    }

    @SneakyThrows
    private static void printB()  {
        while (true) {
            synchronized (object) {
                if (i % 3 == 1) {
                    System.out.print("B");
                    i++;
                    object.notifyAll();
                } else {
                    object.wait();
                }
            }
        }
    }

    @SneakyThrows
    private static void printC()  {
        while (true) {
            synchronized (object) {
                if (i % 3 == 2) {
                    System.out.print("C");
                    i++;
                    object.notifyAll();
                } else {
                    object.wait();
                }
            }
        }
    }
}
